<template>
  <div class="app-container" ref="appcontainer">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
   <!--   <el-form-item label="父级id" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入父级id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="祖籍列表" prop="ancestors">
        <el-input
          v-model="queryParams.ancestors"
          placeholder="请输入祖籍列表"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="分类名称" prop="classifyName">
        <el-input
          v-model="queryParams.classifyName"
          placeholder="请输入分类名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="分类图标" prop="classifyImg">-->
<!--        <el-input-->
<!--          v-model="queryParams.classifyImg"-->
<!--          placeholder="请输入分类图标"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="是否推荐" prop="recommend">
        <el-select v-model="queryParams.recommend" placeholder="请选择是否推荐" clearable size="small">
          <el-option
            v-for="dict in recommendOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="排序" prop="orderNum">-->
<!--        <el-input-->
<!--          v-model="queryParams.orderNum"-->
<!--          placeholder="请输入排序"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="启用状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择启用状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
        <el-button   size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shopping:classify:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      :height="tableHeight"
      v-loading="loading"
      :data="classifyList"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
<!--      <el-table-column label="父级id" prop="parentId" />-->
<!--      <el-table-column label="祖籍列表" align="center" prop="ancestors" />-->
      <el-table-column label="分类名称" align="center" prop="classifyName" />
      <el-table-column label="分类图标" align="center" prop="classifyImg" >
        <template width="120" slot-scope="scope">
          <img style="width:60px;height:60px;border:none;" :src="scope.row.classifyImg">
        </template>
      </el-table-column>
      <el-table-column label="是否推荐" align="center" prop="recommend">
        <template slot-scope="scope">
          <dict-tag :options="recommendOptions" :value="scope.row.recommend"/>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column label="启用状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="statusOptions" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shopping:classify:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleAdd(scope.row)"
            v-hasPermi="['shopping:classify:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shopping:classify:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改商城分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="父级id" prop="parentId">
          <treeselect v-model="form.parentId" :options="classifyOptions" :normalizer="normalizer" placeholder="请选择父级id" />
        </el-form-item>
        <el-form-item label="分类名称" prop="classifyName">
          <el-input v-model="form.classifyName" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="分类图标" prop="classifyImg">
          <imageUpload v-model="form.classifyImg"/>
        </el-form-item>
        <el-form-item label="是否推荐">
          <el-radio-group v-model="form.recommend">
            <el-radio
              v-for="dict in recommendOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="启用状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
       <!-- <el-form-item label="创建来源" prop="createType">
          <el-select v-model="form.createType" placeholder="请选择创建来源">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="更新来源" prop="updateType">
          <el-select v-model="form.updateType" placeholder="请选择更新来源">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listClassify, getClassify, delClassify, addClassify, updateClassify, exportClassify } from "@/api/shopping/classify";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "Classify",
    components: {
      Treeselect
    },
    data() {
      return {
        tableHeight:0,
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 商城分类表格数据
        classifyList: [],
        // 商城分类树选项
        classifyOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否推荐字典
        recommendOptions: [],
        // 启用状态字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          parentId: null,
          ancestors: null,
          classifyName: null,
          classifyImg: null,
          recommend: null,
          orderNum: null,
          status: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
      this.calHeight();
      this.getDicts("sys_yes_no").then(response => {
        this.recommendOptions = response.data;
      });
      this.getDicts("sys_yes_no").then(response => {
        this.statusOptions = response.data;
      });
    },
    methods: {
      calHeight () {
        this.$nextTick(() => {
          const rect = this.$refs.appcontainer.getBoundingClientRect();
          let height= this.$refs.queryForm.$el.offsetHeight;
          this.tableHeight = rect.height-height-120;
        })
      },
      /** 查询商城分类列表 */
      getList() {
        this.loading = true;
        listClassify(this.queryParams).then(response => {
          this.classifyList = this.handleTree(response.data, "id", "parentId");
          this.loading = false;
        });
      },
      /** 转换商城分类数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.classifyName,
          children: node.children
        };
      },
      /** 查询商城分类下拉树结构 */
      getTreeselect() {
        listClassify().then(response => {
          this.classifyOptions = [];
          const data = { id: 0, classifyName: '顶级节点', children: [] };
          data.children = this.handleTree(response.data, "id", "parentId");
          this.classifyOptions.push(data);
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          parentId: null,
          ancestors: null,
          classifyName: null,
          classifyImg: null,
          recommend: "N",
          orderNum: 0,
          status: "Y",
          delState: null,
          delUser: null,
          delType: null,
          delTime: null,
          createBy: null,
          createType: null,
          createTime: null,
          updateBy: null,
          updateType: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        this.getTreeselect();
        if (row != null && row.id) {
          this.form.parentId = row.id;
        } else {
          this.form.parentId = 0;
        }
        this.open = true;
        this.title = "添加商品分类";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.getTreeselect();
        if (row != null) {
          this.form.parentId = row.id;
        }
        getClassify(row.id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改商品分类";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateClassify(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addClassify(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除商品分类?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delClassify(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      }
    }
  };
</script>
